package jwdt.xadmin.finace.action;

import jwdt.common.util.CacheUtil;
import jwdt.xadmin.finace.bean.HaLog;
import jwdt.xadmin.finace.bean.HaUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.MathUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/finace")
public class LogAction extends FinaceAction {

	@RequestMapping("/loglist")
	public Page doLoglist(Form f, Module m) throws Exception {
		// 
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 15);
		String sql = "";
		String account = f.getString("account2");
		if (!"".equals(account)) {
			sql = " and a.account = '" + account + "'" + sql;
			f.addQueryString("account2");
		}
		f.add("accounts", Jdb.get().query(super.sql_selAcc, CacheUtil.C0));
		String logprice = f.getString("logprice2");
		if (!"".equals(logprice)) {
			sql = " and a.logprice = '" + logprice + "'" + sql;
			f.addQueryString("logprice2");
		}
		String logtime2 = f.getString("logtime2");
		if (!"".equals(logtime2)) {
			sql = " and a.logtime >= '" + logtime2 + "'" + sql;
			f.addQueryString("logtime2");
		}
		String logtime3 = f.getString("logtime3");
		if (!"".equals(logtime3)) {
			sql = " and a.logtime <= '" + logtime3 + "'" + sql;
			f.addQueryString("logtime3");
		}
		String type = f.getString("type2");
		if (!"".equals(type)) {
			sql = " and a.type = '" + type + "'" + sql;
			f.addQueryString("type2");
		}
		f.add("types", Jdb.get().query(super.sql_selType, CacheUtil.C0));
		String user = f.getString("user2");
		if (!"".equals(user)) {
			sql = " and a.user = '" + user + "'" + sql;
			f.addQueryString("user2");
		}
		f.add("users", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		String bz = f.getString("bz2");
		if(!"".equals(bz)) {
			sql = " and a.bz like '%" + bz + "%'" + sql;
			f.addQueryString("bz2");
		}
		int zt = f.getInt("zt2", -1);
		if(zt != -1) {
			sql = " a.zt=" + f.getInt("zt2", 1) + sql;
			f.addQueryString("zt2");
		} else {
			sql = " 1=1" + sql;
		}		
		String csql = "SELECT COUNT(a.id) FROM ha_log a WHERE " + sql;
		String ssql = "SELECT a.id,a.logtime,c.name AS 'aname',d.name AS 'uname',b.MXMC AS 'tname',a.bz,a.logprice FROM ha_log a, ha_flmx b, ha_account c, ha_user d WHERE a.type=b.MXDM AND a.account=c.id AND a.user=d.id and " + sql;
		PageUtil pu = new PageUtil(csql, ssql + " order by logtime desc, id desc", page, pageSize);
		//
		String inTotal = "SELECT SUM(a.logprice) FROM ha_log a, ha_flmx b WHERE a.type=b.MXDM AND b.FLDM='income' and " + sql;
		f.add("inTotal", Jdb.get().queryForDouble(inTotal));
		String outTotal = "SELECT SUM(a.logprice) FROM ha_log a, ha_flmx b WHERE a.type=b.MXDM AND b.FLDM!='income' and " + sql;
		f.add("outTotal", Jdb.get().queryForDouble(outTotal));
		//
		f.add("today", DateUtil.getDateTime().substring(0, 10));
		f.add("pager", pu.setUrl(m.getFullPath()+".do").setParams(f.buildQueryMap()));
		return m.defaultPage();
	}

	@RequestMapping("/logadd")
	public Page doLogadd(Form f, Module m) throws Exception {
		f.add("accounts", Jdb.get().query(super.sql_selAcc, CacheUtil.C0));
		f.add("users", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		f.add("types", Jdb.get().query(super.sql_selType, CacheUtil.C0));
		f.add("cdate", DateUtil.getDateTime().substring(0, 10));
		return m.findPage(super.url+"logadd");
	}

	@RequestMapping("/logaddact")
	public Page doLogaddact(Form f, Module m) throws Exception {
		// 
		HaLog obj = (HaLog)f.copy2Class(HaLog.class);
		String ret = "添加成功";
		try {
			Jdb.get().add(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		// 记录支出时显示
		String sql = "SELECT b.FLDM FROM ha_flmx a,ha_fl b WHERE a.fldm=b.fldm AND b.zt=1 AND a.MXDM=?";
		String fldm = (String)Jdb.get().uniqueResult(sql, new Object[]{obj.getType()});
		if(!"income".equals(fldm))
		{
			Double sum = (Double)Jdb.get().uniqueResult("SELECT SUM(a.logprice) FROM ha_log a, jwdt_zd_flmx b WHERE a.type=b.MXDM AND b.FLDM!='income' AND SUBSTRING(logtime, 1, 7)=? AND user=?", new Object[]{DateUtil.getDate(), obj.getUser()});
			if(sum == null)
				sum = 0d;
			HaUser u = (HaUser)Jdb.get().get(HaUser.class, obj.getUser());
			double quota = u.getQuota().doubleValue();
			if(sum > quota) {
				ret += "， 用户" + u.getName() + "限额 " + quota + "元/月，实际使用" + MathUtil.round(sum, 2) + "元，已经超支了哦  :)";
			}
		}
		ret = "【" + obj.getBz() + "】" + ret;
		f.add("ret", ret);
		int savetype = f.getInt("savetype", 0);
		if(savetype == 0) {
			return doLogadd(f, m);
		} else {
			return m.getRedirectPage(super.url + "loglist.do");
		}
	}

	@RequestMapping("/logedit")
	public Page doLogedit(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaLog obj = (HaLog)Jdb.get().get(HaLog.class, id);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		f.add("obj", obj);
		f.add("accounts", Jdb.get().query(super.sql_selAcc, CacheUtil.C0));
		f.add("users", Jdb.get().query(super.sql_selUser, CacheUtil.C0));
		f.add("types", Jdb.get().query(super.sql_selType, CacheUtil.C0));
		return m.defaultPage();
	}

	@RequestMapping("/logeditact")
	public Page doLogeditact(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaLog obj = (HaLog)Jdb.get().get(HaLog.class, id);
		if(obj == null) {
			f.add("ret", "您要修改的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		obj = (HaLog)f.copy2Obj(obj);
		String ret = "修改成功";
		try {
			Jdb.get().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		f.add("ret", "【" + obj.getBz() + "】" + ret);
		return m.getRedirectPage(super.url + "loglist.do");
	}

	@RequestMapping("/logdel")
	public Page doLogdel(Form f, Module m) throws Exception {
		// 
		String id = f.getString("id");
		HaLog obj = (HaLog)Jdb.get().get(HaLog.class, id);
		if(obj == null) {
			f.add("ret", "您要删除的记录不存在：id=" + id);
			return m.findPage("tips");
		}
		String bz = obj.getBz();
		String ret = "删除成功";
		try {
			Jdb.get().del(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除出错";
		}
		ret = "【" + bz + "】" + ret;
		f.add("ret", ret);
		return m.getRedirectPage(super.url + "loglist.do");
	}

}
